package com.cn.goodsadmin.dao.impl;

import com.cn.goodsadmin.dao.BaseDao;
import com.cn.goodsadmin.dao.IUserBuyLogDao;
import com.cn.goodsadmin.po.GoodsInfoLog;
import com.cn.goodsadmin.po.UserFav;
import com.cn.goodsadmin.po.Userbuylog;
import com.cn.goodsadmin.util.TimeUtil;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository("UserBuyLogDaoImpl")
public class UserBuyLogDaoImpl extends BaseDao<Userbuylog> implements IUserBuyLogDao {
    public List<UserFav> findFavByOpenid(String openid, int size, int appid) throws Exception {
        //SELECT d_mama_dish.t_order_info.c_creator , count( * ) AS count
        //FROM  d_mama_dish.t_order_info
        //GROUP BY c_openid
        //ORDER BY count DESC
        //LIMIT 100
        Criteria cri = getSessionFactory().getCurrentSession().createCriteria(Userbuylog.class);
        cri.setProjection(Projections.projectionList()
                .add(Projections.groupProperty("cGoodsid"), "c_goodsid")
                .add(Projections.count("cGoodsid"), "total")
        );
        cri.add(Restrictions.eq("cOpenid", openid));
        cri.addOrder(Order.desc("total"));
        cri.setMaxResults(size);
        //TODO 测试阶段先关闭 打开查询缓存
        //cri.setCacheable(true);
        cri.setResultTransformer(Transformers.aliasToBean(UserFav.class));
        return cri.list();
    }

    public List<Userbuylog> findAllBuyLog(int day, String goodsId, int appid) throws Exception {
        StringBuilder builder=new StringBuilder();
        builder.append("from Userbuylog u where u.cCtime>?0");
        List<Object> paraList=new ArrayList<Object>();
        paraList.add(TimeUtil.getCurrDay(-1*day));
        builder.append(" and c.cCtime<?1");
        paraList.add(TimeUtil.getCurrDay(0));
        builder.append(" and c.cAppid=?2");
        paraList.add(appid);
        if(goodsId!=null) {
            builder.append(" and c.cGoodsid=?3");
            paraList.add(goodsId);
        }
        return find(builder.toString(),paraList.toArray());
    }


    public List<GoodsInfoLog> findAllgoodsCountByTime(int day, int appid) throws Exception {
        Criteria cri = getSessionFactory().getCurrentSession().createCriteria(Userbuylog.class);
        cri.setProjection(Projections.projectionList()
                .add(Projections.groupProperty("cGoodsid"), "goodsId")
                .add(Projections.count("cGoodsid"), "total")

        );
        cri.add(Restrictions.between("cCtime", TimeUtil.getCurrDay(-1 * day), TimeUtil.getCurrDay(0)));
        cri.addOrder(Order.desc("total"));
        cri.add(Restrictions.eq("cAppid",appid));
        //TODO 测试阶段先关闭 打开查询缓存
        //cri.setCacheable(true);
        cri.setResultTransformer(Transformers.aliasToBean(GoodsInfoLog.class));
        return cri.list();
    }


    public List<Userbuylog>  findGoodsCardByTime(int day,String goodsid,int appid)throws Exception{
        StringBuilder builder=new StringBuilder();
        builder.append("from Userbuylog c where c.cCtime>?0");
        List<Object> paraList=new ArrayList<Object>();
        paraList.add(TimeUtil.getCurrDay(-1*day));
        builder.append(" and c.cCtime<?1");
        paraList.add(TimeUtil.getCurrDay(0));
        int index=2;
        if(goodsid!=null) {
            builder.append(" and c.cGoodsid=?2");
            paraList.add(goodsid);
            index++;
        }
        builder.append(" and c.cAppid=?"+index);
        paraList.add(appid);
        return find(builder.toString(),paraList.toArray());
    }

    @Override
    public long findUserAllBuyCount(String openid, int appid) throws Exception {
        StringBuilder builder=new StringBuilder();
        builder.append("select count(*) from  Userbuylog cf where cf.cOpenid='"+openid+"'");
        builder.append(" and cf.cAppid="+appid);
        Long count = (Long)find(builder.toString()).listIterator().next();
        return count.longValue();
    }
}
